package cn.wowjoy.pda.android_pharmacy.mvvm.presenter;

import android.content.Intent;
import android.util.Log;

import cn.wowjoy.pda.android_pharmacy.api.ApiService;
import cn.wowjoy.pda.android_pharmacy.common.core.BasePresenter;
import cn.wowjoy.pda.android_pharmacy.common.core.http.ServiceFactory;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.PreferenceManager;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.model.xml.XmlUtils;
import cn.wowjoy.pda.android_pharmacy.model.xmlBaseResponse.RoleInfoResponseEnvelope;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.employeeInfo.EmployeeInfoBeen;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.employeeInfo.EmployeeNode;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.LoginViewModel;
import cn.wowjoy.pda.android_pharmacy.ui.activity.PharmacyActivity;
import gear.yc.com.gearlibrary.rxjava.rxbus.RxBus;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by panxinyu on 2017/7/13.
 */

public class LoginPresenter extends BasePresenter<LoginViewModel> {
    private ApiService mApiService;
    private MDialog waitDialog;

    @Override
    public void onCreatePresenter() {
        mApiService = (ApiService) ServiceFactory.getInstance().getService(ApiService.class);
    }

    public void loadData(final String employeeID, final String pwd) {
        //TODO:联网检查 用户名和密码       记录个人信息 放入共享参数
//        if (employeeID != null && employeeID.equals("pxy") && pwd != null && pwd.equals("123")) {
//            activity.startActivity(new Intent(activity, PharmacyActivity.class));
//            return;
//        }
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(EmployeeNode.getContent("PDAQ06",employeeID)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }

                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
//                        waitDialog.dismiss();
                        if(!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)){
                            new MyToast(activity).showinfo("工号错误,请重新登录");
                            return;
                        }
                        EmployeeInfoBeen employeeInfoBeen = XmlUtils.getEmployeeInfoBeen(xmlData.body.roleInfoResponse.retXml);
                        if (employeeInfoBeen != null) {
//                            Log.e(TAG, "employeeInfoBeen1: " + employeeInfoBeen.toString());
                                    //记录到共享参数
                                    setPersonal(employeeInfoBeen.getZZZGXM(), employeeID ,employeeInfoBeen.getZZZGID());
                                    //验证通过
                                    activity.startActivity(new Intent(activity, PharmacyActivity.class));
                                    RxBus.getInstance().post(Contants.CLEAR_USER_NAME,1);
                                    return;
                        } else {
                            //没有返回数据
                            new MyToast(context).showinfo("服务器错误");
                        }
                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);

    }

    public void setPersonal(String name, String num ,String id) {
        //TODO:个人信息查询  记录至共享参数中
        PreferenceManager.getInstance().setLUserName(name);
        PreferenceManager.getInstance().setLUserNum(num);
        PreferenceManager.getInstance().setLUserId(id);
//        Log.e(TAG, "setPersonal: "+ PreferenceManager.getInstance().getLUserName() +"  getLUserNum:"+
//                PreferenceManager.getInstance().getLUserNum()+" setLUserId:"+ PreferenceManager.getInstance().getLUserId());
    }
}
